import { defineStore } from 'pinia'
import { ref } from 'vue'

const useToken = defineStore(
  //参数1，持久存储的key
  'token',
  //参数2，setup方法，返回token和updateToken和removeToken两个方法
  () => {
    const token = ref(null)
    const updateToken = val => token.value = val
    const removeToken = () => token.value = null
    return { token, updateToken, removeToken }
  },
  //参数3，持久化存储数据，需要“pinia-plugin-persist”组件
  {
    persist: {
      enabled: true,
      strategies: [
        {
          key: 'token',
          storage: localStorage
        }
      ]
    }
  })
export default useToken
